package com.song.JavaThread_day02.ch2_2;

import java.util.Random;

import org.apache.log4j.Logger;


public class Account {
	
	private Logger logger = Logger.getRootLogger();
	
	private double balance;

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}
	//synchronized
	public  void addAmount(double account)
	{
//		double tmp = balance;
		logger.debug(String.format("add sleep原始数据 %f\n", balance));
		try {
			Thread.sleep((int) Math.rint( Math.random()*100 ));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		tmp+=account;
		logger.debug(String.format("add 添加之前balance %f\n", balance));
		balance= balance + account;
		logger.debug(String.format("add 添加后  balance %f\n", balance));
		System.out.printf("company is add %d and  account is %f \n",1000,balance);
		
	}
	
	//synchronized
	public  void subAmount(double account)
	{
//		double tmp = balance;
		logger.debug(String.format("sub sleep原始数据 %f\n", balance));
		try {
			Thread.sleep((int) Math.rint( Math.random()*100 ));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		tmp-=account;
		logger.debug(String.format("sub 添加之前balance %f\n", balance));
		balance=balance - account;
		logger.debug(String.format("sub 添加后  balance %f\n", balance));
		System.out.printf("bank is sub %d and  account is %f \n",1000,balance);
	}
	
}
